package cn.shyouchi.wuguanjia.android.api.event;

import android.content.Context;
import android.text.TextUtils;

import com.alibaba.fastjson.JSON;
import com.chad.library.adapter.base.entity.MultiItemEntity;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import cn.shyouchi.wuguanjia.android.api.ClientProvider;
import cn.shyouchi.wuguanjia.android.api.UserStorageProvider;
import cn.shyouchi.wuguanjia.android.components.storage.UserStorage;
import cn.shyouchi.wuguanjia.android.model.BaseRequestResult;
import cn.shyouchi.wuguanjia.android.model.OwnerEntity;
import cn.shyouchi.wuguanjia.android.ui.event.EventEntity;
import cn.shyouchi.wuguanjia.android.ui.event.arrange.ArrangeEntity;
import cn.shyouchi.wuguanjia.android.ui.event.arrange.RemoteArrangeEntity;
import cn.shyouchi.wuguanjia.android.ui.event.check.CheckEntity;
import cn.shyouchi.wuguanjia.android.ui.event.checknum.CheckNumEntity;
import cn.shyouchi.wuguanjia.android.ui.event.checknum.CheckNumLineEntity;
import cn.shyouchi.wuguanjia.android.ui.event.checknum.LineNumReq;
import cn.shyouchi.wuguanjia.android.ui.event.checknum.LineNumUpReq;
import cn.shyouchi.wuguanjia.android.ui.event.checkpoint.CheckPointEntity;
import cn.shyouchi.wuguanjia.android.ui.event.handover.HandoverEntity;
import cn.shyouchi.wuguanjia.android.ui.event.labor.AllotPersonEntity;
import cn.shyouchi.wuguanjia.android.ui.event.labor.ApplyLaborEntity;
import cn.shyouchi.wuguanjia.android.ui.event.labor.LaborEntity;
import cn.shyouchi.wuguanjia.android.ui.event.labor.LaborRequestEntity;
import cn.shyouchi.wuguanjia.android.ui.event.labor.PersonEntity;
import cn.shyouchi.wuguanjia.android.ui.event.labor.PersonLaborEntity;
import cn.shyouchi.wuguanjia.android.ui.event.labor.PersonRank;
import cn.shyouchi.wuguanjia.android.ui.event.labor.RankTime;
import cn.shyouchi.wuguanjia.android.ui.event.meet.EventMeetEntity;
import cn.shyouchi.wuguanjia.android.ui.event.post.PostEntity;
import cn.shyouchi.wuguanjia.android.ui.event.proprietor.ProprietorEntity;
import cn.shyouchi.wuguanjia.android.ui.event.rank.EventRankEntity;
import cn.shyouchi.wuguanjia.android.ui.event.report.ReportEntity;
import cn.shyouchi.wuguanjia.android.ui.event.temporary.TemporaryEntity;
import cn.shyouchi.wuguanjia.android.ui.event.train.TrainEntity;
import cn.shyouchi.wuguanjia.android.ui.event.train.VirtualData;
import cn.shyouchi.wuguanjia.android.utils.LogUtil;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;
import retrofit2.Retrofit;
import rx.Observable;
import rx.functions.Func1;
import rx.schedulers.Schedulers;
import top.zibin.luban.Luban;

/**
 * Created by Eenie on 2016/11/14 at 13:41
 * Email: 472279981@qq.com
 * Des:项目设置API
 */

public class EventApi {


    private EventService mService;

    private final long DEFAULT_IMG_SIZE = 1024 * 1024;//上传图片不能大于1M
    //
    private UserStorage mUserStorage;
    private Context mContext;

    public EventApi(Context context) {
        mUserStorage = UserStorageProvider.Default();

        mContext = context;
        Retrofit retrofit = ClientProvider.getDefaultClient().getRetrofit();
        mService = retrofit.create(EventService.class);
    }


    public Observable<BaseRequestResult> addOwner(String id, ProprietorEntity entity) {
        return addOwner(id, entity.getName(), entity.getAge(), entity.getHeight(), entity.getDuty(), entity.getGender(), entity.getWorkTime(), entity.getPlate(), entity.getOther(), entity.getPhone(), entity.getAvatarPath());
    }

    public Observable<BaseRequestResult> upOwner(String id, ProprietorEntity entity) {
        return upOwner(id,
                entity.getId(),
                entity.getName(),
                entity.getAge(),
                entity.getHeight(),
                entity.getDuty(),
                entity.getGender(),
                entity.getWorkTime(),
                entity.getPlate(),
                entity.getOther(),
                entity.getAvatarPath(),
                entity.getPhone());
    }

    public Observable<BaseRequestResult> addOwner(String id,
                                                  String name,
                                                  String age,
                                                  String height,
                                                  String job,
                                                  String sex,
                                                  String workTime,
                                                  String plates,
                                                  String other,
                                                  String phone,
                                                  String headImg) {
//        Map<String, Object> fields = new HashMap<>();
//        fields.put("projectsId", id);
//        fields.put("name", name);
//        fields.put("age", age);
//        fields.put("height", height);
//        fields.put("job", job);
//        fields.put("sex", sex);
//        fields.put("workinghours", workTime);
//        fields.put("numberplates", plates);
//        fields.put("remarks", other);

        final Map<String, RequestBody> paramsMap = new HashMap<>();

        final RequestBody projectsIdBody = RequestBody.create(MediaType.parse("multipart/form-data"), id);
        paramsMap.put("projectsId", projectsIdBody);


        final RequestBody nameBody = RequestBody.create(MediaType.parse("multipart/form-data"), name);
        paramsMap.put("name", nameBody);

        final RequestBody ageBody = RequestBody.create(MediaType.parse("multipart/form-data"), age);
        paramsMap.put("age", ageBody);

        final RequestBody heightBody = RequestBody.create(MediaType.parse("multipart/form-data"), height);
        paramsMap.put("height", heightBody);


        final RequestBody jobBody = RequestBody.create(MediaType.parse("multipart/form-data"), job);
        paramsMap.put("job", jobBody);


        final RequestBody sexBody = RequestBody.create(MediaType.parse("multipart/form-data"), sex);
        paramsMap.put("sex", sexBody);

        final RequestBody workinghoursBody = RequestBody.create(MediaType.parse("multipart/form-data"), workTime);
        paramsMap.put("workinghours", workinghoursBody);


        final RequestBody numberplatesBody = RequestBody.create(MediaType.parse("multipart/form-data"), plates);
        paramsMap.put("numberplates", numberplatesBody);


        RequestBody phoneBody = RequestBody.create(MediaType.parse("multipart/form-data"), phone);
        paramsMap.put("phone", phoneBody);

        RequestBody remarksBody = null;

        if (!TextUtils.isEmpty(other)) {
            remarksBody = RequestBody.create(MediaType.parse("multipart/form-data"), other);
            paramsMap.put("remarks", remarksBody);
        }

        final RequestBody finalRemarksBody = remarksBody;
        if (!TextUtils.isEmpty(headImg)) {
            File file = new File(headImg);
            return Luban.get(mContext)
                    .load(file)
                    .putGear(Luban.THIRD_GEAR)
                    .asObservable()
                    .flatMap(new Func1<File, Observable<BaseRequestResult>>() {
                        @Override
                        public Observable<BaseRequestResult> call(File file) {
                            MultipartBody.Part body = null;
                            RequestBody requestFile = RequestBody.create(MediaType.parse("multipart/form-data"), file);
                            body = MultipartBody.Part.createFormData("image", file.getName(), requestFile);
                            return mService.addOwner(mUserStorage.getToken(), paramsMap, body);
                        }
                    });

        } else {
            return mService.addOwner(mUserStorage.getToken(), projectsIdBody, nameBody, ageBody, heightBody, jobBody, sexBody, workinghoursBody, numberplatesBody, finalRemarksBody, null);
        }


    }


    public Observable<OwnerEntity> getOwnerList(String id) {
        return mService.getOwnerList(mUserStorage.getToken(), id);
    }

    public Observable<BaseRequestResult> delOwner(String id) {
        return mService.delOwner(mUserStorage.getToken(), id);
    }

    public Observable<BaseRequestResult> upOwner(String projectid,
                                                 String id,
                                                 String name,
                                                 String age,
                                                 String height,
                                                 String job,
                                                 String sex,
                                                 String workinghours,
                                                 String numberplates,
                                                 String remark,
                                                 String image,
                                                 String phone) {

//        Map<String, Object> params = new HashMap<>();
//        params.put("projectsId", projectid);
//        params.put("id", id);
//        params.put("name", name);
//        params.put("image", image);
//        params.put("age", age);
//        params.put("height", height);
//        params.put("job", job);
//        params.put("sex", sex);
//        params.put("workingours", workingours);
//        params.put("numberplates", numberplates);
//        params.put("remark", remark);
//        params.put("phone", phone);

        final Map<String, RequestBody> paramsMap = new HashMap<>();
        final RequestBody projectsIdBody = RequestBody.create(MediaType.parse("multipart/form-data"), projectid);
        paramsMap.put("projectsId", projectsIdBody);
        final RequestBody IdBody = RequestBody.create(MediaType.parse("multipart/form-data"), id);
        paramsMap.put("id", IdBody);
        final RequestBody nameBody = RequestBody.create(MediaType.parse("multipart/form-data"), name);
        paramsMap.put("name", nameBody);
        final RequestBody ageBody = RequestBody.create(MediaType.parse("multipart/form-data"), age);
        paramsMap.put("age", ageBody);
        final RequestBody heightBody = RequestBody.create(MediaType.parse("multipart/form-data"), height);
        paramsMap.put("height", heightBody);
        final RequestBody jobBody = RequestBody.create(MediaType.parse("multipart/form-data"), job);
        paramsMap.put("job", jobBody);
        final RequestBody sexBody = RequestBody.create(MediaType.parse("multipart/form-data"), sex);
        paramsMap.put("sex", sexBody);
        final RequestBody workinghoursBody = RequestBody.create(MediaType.parse("multipart/form-data"), workinghours);
        paramsMap.put("workinghours", workinghoursBody);
        final RequestBody numberplatesBody = RequestBody.create(MediaType.parse("multipart/form-data"), numberplates);
        paramsMap.put("numberplates", numberplatesBody);

        RequestBody remarksBody = RequestBody.create(MediaType.parse("multipart/form-data"), remark);
        paramsMap.put("remarks", remarksBody);
        RequestBody phoneBody = RequestBody.create(MediaType.parse("multipart/form-data"), phone);
        paramsMap.put("phone", phoneBody);

        if (!TextUtils.isEmpty(image)) {
            File file = new File(image);
            return Luban.get(mContext)
                    .load(file)
                    .putGear(Luban.THIRD_GEAR)
                    .asObservable()
                    .flatMap(new Func1<File, Observable<BaseRequestResult>>() {
                        @Override
                        public Observable<BaseRequestResult> call(File file) {
                            MultipartBody.Part body = null;
                            RequestBody requestFile = RequestBody.create(MediaType.parse("multipart/form-data"), file);
                            body = MultipartBody.Part.createFormData("image", file.getName(), requestFile);
                            return mService.upOwner(mUserStorage.getToken(), paramsMap, body);
                        }
                    });

        } else {
            return mService.upOwner(mUserStorage.getToken(), paramsMap, null);
        }


    }

    public Observable<EventEntity> getProjectList() {
        return mService.getProjectList(mUserStorage.getToken());
    }


    public Observable<HandoverEntity> getPrecautionList(String id) {
        return mService.getPrecautionList(mUserStorage.getToken(), id);
    }

    /**
     * 添加交接班事项
     *
     * @param projectId
     * @param title
     * @param content
     * @param imgPaths
     * @return
     */
    public Observable<BaseRequestResult> addPrecaution(String projectId, String title, String content, List<String> imgPaths) {
        final Map<String, RequestBody> imgMap = new HashMap<>();
        final List<MultipartBody.Part> partHashMap = new ArrayList<>();

        imgMap.put("projectid", RequestBody.create(MediaType.parse("multipart/form-data"), projectId));
        imgMap.put("mattername", RequestBody.create(MediaType.parse("multipart/form-data"), title));
        imgMap.put("matter", RequestBody.create(MediaType.parse("multipart/form-data"), content));

        return Observable.from(imgPaths)
                .subscribeOn(Schedulers.io())
                .flatMap(new Func1<String, Observable<File>>() {
                    @Override
                    public Observable<File> call(String path) {
                        return Luban.get(mContext)
                                .load(new File(path))
                                .putGear(Luban.THIRD_GEAR)
                                .asObservable();
                    }
                })
                .map(new Func1<File, MultipartBody.Part>() {
                    @Override
                    public MultipartBody.Part call(File file) {
                        RequestBody requestFile = RequestBody.create(MediaType.parse("multipart/form-data"), file);
                        MultipartBody.Part body = MultipartBody.Part.createFormData("image[]", file.getName(), requestFile);
                        return body;
                    }
                })
                .toList()
                .flatMap(new Func1<List<MultipartBody.Part>, Observable<BaseRequestResult>>() {
                    @Override
                    public Observable<BaseRequestResult> call(List<MultipartBody.Part> parts) {
                        partHashMap.addAll(parts);
                        return mService.addPrecaution(mUserStorage.getToken(), imgMap, partHashMap);
                    }
                });
    }

    public Observable<BaseRequestResult> upPrecaution(String projectId, String id, String title, String content, List<String> imgPaths) {

        final Map<String, RequestBody> imgMap = new HashMap<>();
        final List<MultipartBody.Part> partHashMap = new ArrayList<>();

        imgMap.put("projectid", RequestBody.create(MediaType.parse("multipart/form-data"), projectId));
        imgMap.put("id", RequestBody.create(MediaType.parse("multipart/form-data"), id));
        imgMap.put("mattername", RequestBody.create(MediaType.parse("multipart/form-data"), title));
        imgMap.put("matter", RequestBody.create(MediaType.parse("multipart/form-data"), content));

        return Observable.from(imgPaths)
                .subscribeOn(Schedulers.io())
                .flatMap(new Func1<String, Observable<File>>() {
                    @Override
                    public Observable<File> call(String path) {
                        return Luban.get(mContext)
                                .load(new File(path))
                                .putGear(Luban.THIRD_GEAR)
                                .asObservable();
                    }
                })
                .map(new Func1<File, MultipartBody.Part>() {
                    @Override
                    public MultipartBody.Part call(File file) {
                        RequestBody requestFile = RequestBody.create(MediaType.parse("multipart/form-data"), file);
                        MultipartBody.Part body = MultipartBody.Part.createFormData("image[]", file.getName(), requestFile);
                        return body;
                    }
                })
                .toList()
                .flatMap(new Func1<List<MultipartBody.Part>, Observable<BaseRequestResult>>() {
                    @Override
                    public Observable<BaseRequestResult> call(List<MultipartBody.Part> parts) {
                        partHashMap.addAll(parts);
                        return mService.upPrecaution(mUserStorage.getToken(), imgMap, partHashMap);
                    }
                });
    }


    public Observable<BaseRequestResult> delPrecaution(String id) {
        return mService.delPrecaution(mUserStorage.getToken(), id);
    }

    public Observable<PostEntity> getPostList(String projectId) {
        return mService.getPostList(mUserStorage.getToken(), projectId);
    }

    public Observable<BaseRequestResult> delPost(String id) {
        return mService.delPost(mUserStorage.getToken(), id);
    }

    public Observable<BaseRequestResult> addPost(String id, String title, String content) {
        Map<String, String> paramMap = new HashMap<>();
        paramMap.put("projectid", id);
        paramMap.put("post", title);
        paramMap.put("jobassignment", content);
        return mService.addPost(mUserStorage.getToken(), paramMap);
    }

    public Observable<BaseRequestResult> upPost(String projectid, String id, String title, String content) {
        Map<String, String> paramMap = new HashMap<>();
        paramMap.put("projectid", projectid);
        paramMap.put("id", id);
        paramMap.put("post", title);
        paramMap.put("jobassignment", content);
        return mService.upPost(mUserStorage.getToken(), paramMap);
    }


    public Observable<TrainEntity> getTrain(String id) {
        return mService.getTrain(mUserStorage.getToken(), id);
    }

    public Observable<BaseRequestResult> delTrain(String id) {
        return mService.delTrain(mUserStorage.getToken(), id);
    }

    /**
     * 添加岗位培训
     *
     * @param projectId
     * @param title
     * @param content
     * @param imgPaths
     * @return
     */
    public Observable<BaseRequestResult> addTrain(String projectId, String title, String content, List<String> imgPaths) {
        final Map<String, RequestBody> imgMap = new HashMap<>();
        final List<MultipartBody.Part> partHashMap = new ArrayList<>();

        imgMap.put("projectid", RequestBody.create(MediaType.parse("multipart/form-data"), projectId));
        imgMap.put("trainingname", RequestBody.create(MediaType.parse("multipart/form-data"), title));
        imgMap.put("trainingcontent", RequestBody.create(MediaType.parse("multipart/form-data"), content));

//        for (String path : imgPaths) {
//            File file = new File(path);
//            RequestBody requestFile = RequestBody.create(MediaType.parse("multipart/form-data"), file);
//            MultipartBody.Part body = MultipartBody.Part.createFormData("image[]", file.getName(), requestFile);
//            partHashMap.add(body);
//        }


        return Observable.from(imgPaths)
                .subscribeOn(Schedulers.io())
                .flatMap(new Func1<String, Observable<File>>() {
                    @Override
                    public Observable<File> call(String path) {
                        File file = new File(path);
                        LogUtil.e("file size is " + file.length());
                        if (file.length() > DEFAULT_IMG_SIZE) {
                            LogUtil.e("file start compress , it is too big!");
                            return Luban.get(mContext)
                                    .load(file)
                                    .putGear(Luban.THIRD_GEAR)
                                    .asObservable();
                        } else {
                            LogUtil.e("file not need compress!");
                            return Observable.just(file);
                        }
                    }
                })
                .map(new Func1<File, MultipartBody.Part>() {
                    @Override
                    public MultipartBody.Part call(File file) {
                        RequestBody requestFile = RequestBody.create(MediaType.parse("multipart/form-data"), file);
                        MultipartBody.Part body = MultipartBody.Part.createFormData("image[]", file.getName(), requestFile);
                        return body;
                    }
                })
                .toList()
                .flatMap(new Func1<List<MultipartBody.Part>, Observable<BaseRequestResult>>() {
                    @Override
                    public Observable<BaseRequestResult> call(List<MultipartBody.Part> parts) {
                        partHashMap.addAll(parts);
                        return mService.addTrain(mUserStorage.getToken(), imgMap, partHashMap);
                    }
                });

    }

    public Observable<BaseRequestResult> upTrain(String projectId,
                                                 String id,
                                                 String title,
                                                 String content,
                                                 List<String> imgPaths) {
        final Map<String, RequestBody> imgMap = new HashMap<>();
        final List<MultipartBody.Part> partHashMap = new ArrayList<>();

        imgMap.put("projectid", RequestBody.create(MediaType.parse("multipart/form-data"), projectId));
        imgMap.put("id", RequestBody.create(MediaType.parse("multipart/form-data"), id));
        imgMap.put("trainingname", RequestBody.create(MediaType.parse("multipart/form-data"), title));
        imgMap.put("trainingcontent", RequestBody.create(MediaType.parse("multipart/form-data"), content));

//        for (String path : imgPaths) {
//            File file = new File(path);
//            RequestBody requestFile = RequestBody.create(MediaType.parse("multipart/form-data"), file);
//            MultipartBody.Part body = MultipartBody.Part.createFormData("image[]", file.getName(), requestFile);
//            partHashMap.add(body);
//        }


        return Observable.from(imgPaths)
                .subscribeOn(Schedulers.io())
                .flatMap(new Func1<String, Observable<File>>() {
                    @Override
                    public Observable<File> call(String path) {
                        File file = new File(path);
                        LogUtil.e("file size is " + file.length());
                        if (file.length() > DEFAULT_IMG_SIZE) {
                            LogUtil.e("file start compress , it is too big!");
                            return Luban.get(mContext)
                                    .load(file)
                                    .putGear(Luban.THIRD_GEAR)
                                    .asObservable();
                        } else {
                            LogUtil.e("file not need compress!");
                            return Observable.just(file);
                        }
                    }
                })
                .map(new Func1<File, MultipartBody.Part>() {
                    @Override
                    public MultipartBody.Part call(File file) {
                        RequestBody requestFile = RequestBody.create(MediaType.parse("multipart/form-data"), file);
                        MultipartBody.Part body = MultipartBody.Part.createFormData("image[]", file.getName(), requestFile);
                        return body;
                    }
                })
                .toList()
                .flatMap(new Func1<List<MultipartBody.Part>, Observable<BaseRequestResult>>() {
                    @Override
                    public Observable<BaseRequestResult> call(List<MultipartBody.Part> parts) {
                        partHashMap.addAll(parts);
                        return mService.upTrain(mUserStorage.getToken(), imgMap, partHashMap);
                    }
                });

    }


    public Observable<ArrangeEntity> getArrange(String id) {
        return mService.getArrangeList(mUserStorage.getToken(), id);
    }

    public Observable<BaseRequestResult> delArrange(String id) {
        return mService.delArrange(mUserStorage.getToken(), id);
    }


    public Observable<BaseRequestResult> addArrange(String projectid, String title, String startTime, String endTime) {

        Map<String, String> paramMap = new HashMap<>();
        paramMap.put("projectid", projectid);
        paramMap.put("servicesname", title);
        paramMap.put("starttime", startTime);
        paramMap.put("endtime", endTime);
        return mService.addArrange(mUserStorage.getToken(), paramMap);
    }

    public Observable<EventRankEntity> getRankList(String id) {
        return mService.getRankList(mUserStorage.getToken(), id);
    }

    public Observable<BaseRequestResult> delRank(String id) {
        return mService.delRank(mUserStorage.getToken(), id);
    }


    public Observable<BaseRequestResult> addRank(String projectid, String title, String startTime, String endTime) {
        Map<String, String> paramMap = new HashMap<>();
        paramMap.put("projectid", projectid);
        paramMap.put("servicesname", title);
        paramMap.put("starttime", startTime);
        paramMap.put("endtime", endTime);
        return mService.addRank(mUserStorage.getToken(), paramMap);
    }

    public Observable<BaseRequestResult> upRank(String projectid, String id, String title, String startTime, String endTime) {
        Map<String, String> paramMap = new HashMap<>();
        paramMap.put("projectid", projectid);
        paramMap.put("id", id);
        paramMap.put("servicesname", title);
        paramMap.put("starttime", startTime);
        paramMap.put("endtime", endTime);
        return mService.upRank(mUserStorage.getToken(), paramMap);
    }


    public Observable<BaseRequestResult> addTemporary(String projectid,
                                                      String title,
                                                      String projectleader,
                                                      String number,
                                                      String startTime,
                                                      String endTime,
                                                      String content,
                                                      String phone,
                                                      String address,
                                                      String owner_company,
                                                      String owner_contacts,
                                                      String owner_phone

    ) {
        Map<String, String> paramMap = new HashMap<>();
        paramMap.put("projectid", projectid);
        paramMap.put("projectname", title);
        paramMap.put("projectleader", projectleader);
        paramMap.put("number", number);
        paramMap.put("startingtime", startTime);
        paramMap.put("endtime", endTime);
        paramMap.put("description", content);
        paramMap.put("phone", phone);
        paramMap.put("address", address);
        paramMap.put("owner_company", owner_company);
        paramMap.put("owner_contacts", owner_contacts);
        paramMap.put("owner_phone", owner_phone);


        return mService.addTemporary(mUserStorage.getToken(), paramMap);
    }

    public Observable<BaseRequestResult> upTemporary(String projectid,
                                                     String id,
                                                     String title,
                                                     String projectleader,
                                                     String number,
                                                     String startTime,
                                                     String endTime,
                                                     String content,
                                                     String phone,
                                                     String address,
                                                     String owner_company,
                                                     String owner_contacts,
                                                     String owner_phone) {

        Map<String, String> paramMap = new HashMap<>();
        paramMap.put("projectid", projectid);
        paramMap.put("id", id);
        paramMap.put("projectname", title);
        paramMap.put("projectleader", projectleader);
        paramMap.put("number", number);
        paramMap.put("startingtime", startTime);
        paramMap.put("endtime", endTime);
        paramMap.put("description", content);
        paramMap.put("phone", phone);
        paramMap.put("address", address);

        paramMap.put("owner_company", owner_company);
        paramMap.put("owner_contacts", owner_contacts);
        paramMap.put("owner_phone", owner_phone);

        return mService.upTemporary(mUserStorage.getToken(), paramMap);
    }

    public Observable<BaseRequestResult> delTemporary(String id) {
        return mService.delTemporary(mUserStorage.getToken(), id);
    }

    public Observable<TemporaryEntity> getTemporaryList(String id) {
        return mService.getTemporaryList(mUserStorage.getToken(), id);
    }


    public Observable<BaseRequestResult> addCheck(String projectid,
                                                  String area,
                                                  String address,
                                                  String attendance,
                                                  String attendancescope,
                                                  String fingerprinttime,
                                                  String contracttime,
                                                  String longitude,
                                                  String latitude) {
        Map<String, String> paramMap = new HashMap<>();
        paramMap.put("projectid", projectid);
        paramMap.put("area", area);
        paramMap.put("address", address);
        paramMap.put("attendance", attendance);
        paramMap.put("attendancescope", attendancescope);
        paramMap.put("fingerprinttime", fingerprinttime);
        paramMap.put("contracttime", contracttime);
        paramMap.put("longitude", longitude);
        paramMap.put("latitude", latitude);
        return mService.addCheck(mUserStorage.getToken(), paramMap);
    }

    public Observable<BaseRequestResult> upCheck(String projectid,
                                                 String area,
                                                 String address,
                                                 String attendance,
                                                 String attendancescope,
                                                 String fingerprinttime,
                                                 String contracttime,
                                                 String longitude,
                                                 String latitude) {

        Map<String, String> paramMap = new HashMap<>();
        paramMap.put("projectid", projectid);
        paramMap.put("area", area);
        paramMap.put("address", address);
        paramMap.put("attendance", attendance);
        paramMap.put("attendancescope", attendancescope);
        paramMap.put("fingerprinttime", fingerprinttime);
        paramMap.put("contracttime", contracttime);
        paramMap.put("longitude", longitude);
        paramMap.put("latitude", latitude);
        return mService.upCheck(mUserStorage.getToken(), paramMap);
    }


    public Observable<CheckEntity> getCheckList(String id) {
        return mService.getCheckList(mUserStorage.getToken(), id);
    }

    public Observable<CheckPointEntity> getPointList(String id) {
        return mService.getPointList(mUserStorage.getToken(), id);
    }


    /**
     * 添加巡检点位
     *
     * @param projectId
     * @param title
     * @param inspectionmethod
     * @param content
     * @param imgPaths
     * @return
     */
    public Observable<BaseRequestResult> addPoint(String projectId, String title, String inspectionmethod, String content, List<String> imgPaths) {
        final Map<String, RequestBody> imgMap = new HashMap<>();
        final List<MultipartBody.Part> partHashMap = new ArrayList<>();

        imgMap.put("projectid", RequestBody.create(MediaType.parse("multipart/form-data"), projectId));
        imgMap.put("inspectionname", RequestBody.create(MediaType.parse("multipart/form-data"), title));
        imgMap.put("inspectionmethod", RequestBody.create(MediaType.parse("multipart/form-data"), inspectionmethod));
        imgMap.put("inspectioncontent", RequestBody.create(MediaType.parse("multipart/form-data"), content));

//        for (String path : imgPaths) {
//            File file = new File(path);
//            RequestBody requestFile = RequestBody.create(MediaType.parse("multipart/form-data"), file);
//            MultipartBody.Part body = MultipartBody.Part.createFormData("image[]", file.getName(), requestFile);
//            partHashMap.add(body);
//        }

        return Observable.from(imgPaths)
                .subscribeOn(Schedulers.io())
                .flatMap(new Func1<String, Observable<File>>() {
                    @Override
                    public Observable<File> call(String path) {
                        File file = new File(path);
                        LogUtil.e("file size is " + file.length());
                        if (file.length() > DEFAULT_IMG_SIZE) {
                            LogUtil.e("file start compress , it is too big!");
                            return Luban.get(mContext)
                                    .load(file)
                                    .putGear(Luban.THIRD_GEAR)
                                    .asObservable();
                        } else {
                            LogUtil.e("file not need compress!");
                            return Observable.just(file);
                        }
                    }
                })
                .map(new Func1<File, MultipartBody.Part>() {
                    @Override
                    public MultipartBody.Part call(File file) {
                        RequestBody requestFile = RequestBody.create(MediaType.parse("multipart/form-data"), file);
                        MultipartBody.Part body = MultipartBody.Part.createFormData("image[]", file.getName(), requestFile);
                        return body;
                    }
                })
                .toList()
                .flatMap(new Func1<List<MultipartBody.Part>, Observable<BaseRequestResult>>() {
                    @Override
                    public Observable<BaseRequestResult> call(List<MultipartBody.Part> parts) {
                        partHashMap.addAll(parts);
                        return mService.addPoint(mUserStorage.getToken(), imgMap, partHashMap);
                    }
                });


    }

    public Observable<BaseRequestResult> upPoint(String projectId,
                                                 String id,
                                                 String title,
                                                 String inspectionmethod,
                                                 String content,
                                                 List<String> imgPaths) {
        final Map<String, RequestBody> imgMap = new HashMap<>();
        final List<MultipartBody.Part> partHashMap = new ArrayList<>();

        imgMap.put("projectid", RequestBody.create(MediaType.parse("multipart/form-data"), projectId));
        imgMap.put("id", RequestBody.create(MediaType.parse("multipart/form-data"), id));
        imgMap.put("inspectionname", RequestBody.create(MediaType.parse("multipart/form-data"), title));
        imgMap.put("inspectionmethod", RequestBody.create(MediaType.parse("multipart/form-data"), inspectionmethod));
        imgMap.put("inspectioncontent", RequestBody.create(MediaType.parse("multipart/form-data"), content));

//        for (String path : imgPaths) {
//            File file = new File(path);
//            RequestBody requestFile = RequestBody.create(MediaType.parse("multipart/form-data"), file);
//            MultipartBody.Part body = MultipartBody.Part.createFormData("image[]", file.getName(), requestFile);
//            partHashMap.add(body);
//        }

        return Observable.from(imgPaths)
                .subscribeOn(Schedulers.io())
                .flatMap(new Func1<String, Observable<File>>() {
                    @Override
                    public Observable<File> call(String path) {
                        File file = new File(path);
                        LogUtil.e("file size is " + file.length());
                        if (file.length() > DEFAULT_IMG_SIZE) {
                            LogUtil.e("file start compress , it is too big!");
                            return Luban.get(mContext)
                                    .load(file)
                                    .putGear(Luban.THIRD_GEAR)
                                    .asObservable();
                        } else {
                            LogUtil.e("file not need compress!");
                            return Observable.just(file);
                        }
                    }
                })
                .map(new Func1<File, MultipartBody.Part>() {
                    @Override
                    public MultipartBody.Part call(File file) {
                        RequestBody requestFile = RequestBody.create(MediaType.parse("multipart/form-data"), file);
                        MultipartBody.Part body = MultipartBody.Part.createFormData("image[]", file.getName(), requestFile);
                        return body;
                    }
                })
                .toList()
                .flatMap(new Func1<List<MultipartBody.Part>, Observable<BaseRequestResult>>() {
                    @Override
                    public Observable<BaseRequestResult> call(List<MultipartBody.Part> parts) {
                        partHashMap.addAll(parts);
                        return mService.upPoint(mUserStorage.getToken(), imgMap, partHashMap);
                    }
                });
    }

    public Observable<BaseRequestResult> sortPoint(String projectId,
                                                   List<CheckPointEntity.ResultMessageBean> ids) {
        final List<MultipartBody.Part> partHashMap = new LinkedList<>();
        MultipartBody.Part body = MultipartBody.Part.createFormData("projectid", projectId);
        partHashMap.add(body);
        return Observable.from(ids)
                .subscribeOn(Schedulers.io())
                .map(new Func1<CheckPointEntity.ResultMessageBean, MultipartBody.Part>() {
                    @Override
                    public MultipartBody.Part call(CheckPointEntity.ResultMessageBean bean) {
                        return MultipartBody.Part.createFormData("id[]", String.valueOf(bean.getId()));
                    }
                })
                .toList()
                .flatMap(new Func1<List<MultipartBody.Part>, Observable<BaseRequestResult>>() {
                    @Override
                    public Observable<BaseRequestResult> call(List<MultipartBody.Part> parts) {
                        partHashMap.addAll(parts);
                        return mService.sortPoint(mUserStorage.getToken(), partHashMap);
                    }
                });
    }


    public Observable<BaseRequestResult> delPoint(String id) {
        return mService.delPoint(mUserStorage.getToken(), id);
    }


    public Observable<BaseRequestResult> delCheckNum(String id) {
        return mService.delCheckNum(mUserStorage.getToken(), id);
    }


    public Observable<BaseRequestResult> addCheckNumLine(String projectId, String name, String difference) {
        final Map<String, Object> params = new HashMap<>();
        params.put("projectid", projectId);
        params.put("name", name);
        params.put("difference", difference);
        return mService.addCheckNumLine(mUserStorage.getToken(), params);
    }

    public Observable<BaseRequestResult> upCheckNumLine(String projectId, String id, String name, String difference) {

        final Map<String, Object> params = new HashMap<>();
        params.put("projectid", projectId);
        params.put("id", id);
        params.put("name", name);
        params.put("difference", difference);
        return mService.upCheckNumLine(mUserStorage.getToken(), params);
    }


    public Observable<BaseRequestResult> delCheckNumLine(String id) {
        return mService.delCheckNumLine(mUserStorage.getToken(), id);
    }


    public Observable<EventMeetEntity> getMeetList(String id) {
        return mService.getMeetList(mUserStorage.getToken(), id);
    }


    public Observable<BaseRequestResult> addMeet(String projectId,
                                                 String serviceid,
                                                 String meetingplace,
                                                 String meetingtime,
                                                 String content,
                                                 List<String> imgPaths) {
        final Map<String, RequestBody> imgMap = new HashMap<>();
        final List<MultipartBody.Part> partHashMap = new ArrayList<>();

        imgMap.put("projectid", RequestBody.create(MediaType.parse("multipart/form-data"), projectId));
        imgMap.put("serviceid", RequestBody.create(MediaType.parse("multipart/form-data"), serviceid));


        imgMap.put("meetingplace", RequestBody.create(MediaType.parse("multipart/form-data"), meetingplace));
        imgMap.put("meetingtime", RequestBody.create(MediaType.parse("multipart/form-data"), meetingtime));
        imgMap.put("meetingcontent", RequestBody.create(MediaType.parse("multipart/form-data"), content));


        return Observable.from(imgPaths)
                .subscribeOn(Schedulers.io())
                .flatMap(new Func1<String, Observable<File>>() {
                    @Override
                    public Observable<File> call(String path) {
                        File file = new File(path);
                        LogUtil.e("file size is " + file.length());
                        if (file.length() > DEFAULT_IMG_SIZE) {
                            LogUtil.e("file start compress , it is too big!");
                            return Luban.get(mContext)
                                    .load(file)
                                    .putGear(Luban.THIRD_GEAR)
                                    .asObservable();
                        } else {
                            LogUtil.e("file not need compress!");
                            return Observable.just(file);
                        }
                    }
                })
                .map(new Func1<File, MultipartBody.Part>() {
                    @Override
                    public MultipartBody.Part call(File file) {
                        RequestBody requestFile = RequestBody.create(MediaType.parse("multipart/form-data"), file);
                        MultipartBody.Part body = MultipartBody.Part.createFormData("image[]", file.getName(), requestFile);
                        return body;
                    }
                })
                .toList()
                .flatMap(new Func1<List<MultipartBody.Part>, Observable<BaseRequestResult>>() {
                    @Override
                    public Observable<BaseRequestResult> call(List<MultipartBody.Part> parts) {
                        partHashMap.addAll(parts);
                        return mService.addMeet(mUserStorage.getToken(), imgMap, partHashMap);
                    }
                });

//        for (String path : imgPaths) {
//            File file = new File(path);
//            RequestBody requestFile = RequestBody.create(MediaType.parse("multipart/form-data"), file);
//            MultipartBody.Part body = MultipartBody.Part.createFormData("image[]", file.getName(), requestFile);
//            partHashMap.add(body);
//        }


    }


    public Observable<BaseRequestResult> delMeet(String id) {
        return mService.delMeet(mUserStorage.getToken(), id);
    }


    public Observable<BaseRequestResult> addReport(String projectId, String postsettingid, String serviceid, String jetlag, List<String> reportingtime) {
        Map<String, String> imgMap = new HashMap<>();
        imgMap.put("projectid", projectId);
        imgMap.put("postsettingid", postsettingid);
        imgMap.put("serviceid", serviceid);
        imgMap.put("jetlag", jetlag);
        imgMap.put("reportingtime", JSON.toJSONString(reportingtime));


        return mService.addReport(mUserStorage.getToken(), imgMap);
    }

    public Observable<BaseRequestResult> upReport(String projectId, String id, String postsettingid, String serviceid, String jetlag, List<String> reportingtime) {
        Map<String, String> imgMap = new HashMap<>();
        imgMap.put("projectid", projectId);
        imgMap.put("id", id);
        imgMap.put("postsettingid", postsettingid);
        imgMap.put("serviceid", serviceid);
        imgMap.put("jetlag", jetlag);
        imgMap.put("reportingtime", JSON.toJSONString(reportingtime));
        return mService.upReport(mUserStorage.getToken(), imgMap);
    }

    public Observable<ReportEntity> getReportList(String id) {
        return mService.getReportList(mUserStorage.getToken(), id);
    }

    public Observable<BaseRequestResult> delReport(String id) {
        return mService.delReport(mUserStorage.getToken(), id);
    }


    public Observable<BaseRequestResult> addPointNum(String projectId, String lineId, List<CheckPointEntity.ResultMessageBean> points) {

        LineNumReq req = new LineNumReq();
        req.setProjectid(projectId);
        req.setLineid(lineId);
        req.setInspections(points);


//        List<MultipartBody.Part> partHashMap = new ArrayList<>();
//        partHashMap.add(MultipartBody.Part.createFormData("projectid", projectId));
//        for (CheckPointEntity.ResultMessageBean path : points) {
//            MultipartBody.Part Inspectionpoint = MultipartBody.Part.createFormData("inspectionpoint[]", String.valueOf(path.getId()));
//            MultipartBody.Part Inspectiontime = MultipartBody.Part.createFormData("inspectiontime[]", path.getTime());
//            partHashMap.add(Inspectionpoint);
//            partHashMap.add(Inspectiontime);
//        }


        return mService.addPointNum(mUserStorage.getToken(), JSON.toJSONString(req));
    }

    public Observable<BaseRequestResult> upPointNum(String id, List<CheckPointEntity.ResultMessageBean> points) {

        LineNumUpReq req = new LineNumUpReq();
        req.setInspectiondayid(id);
        req.setInspections(points);


//        List<MultipartBody.Part> partHashMap = new ArrayList<>();
//        partHashMap.add(MultipartBody.Part.createFormData("projectid", projectId));
//        for (CheckPointEntity.ResultMessageBean path : points) {
//            MultipartBody.Part Inspectionpoint = MultipartBody.Part.createFormData("inspectionpoint[]", String.valueOf(path.getId()));
//            MultipartBody.Part Inspectiontime = MultipartBody.Part.createFormData("inspectiontime[]", path.getTime());
//            partHashMap.add(Inspectionpoint);
//            partHashMap.add(Inspectiontime);
//        }


//        List<MultipartBody.Part> partHashMap = new ArrayList<>();
//        partHashMap.add(MultipartBody.Part.createFormData("projectid", projectId));
//        partHashMap.add(MultipartBody.Part.createFormData("id", id));
//        for (CheckPointEntity.ResultMessageBean path : points) {
//            MultipartBody.Part Inspectionpoint = MultipartBody.Part.createFormData("inspectionpoint[]", String.valueOf(path.getId()));
//            MultipartBody.Part Inspectiontime = MultipartBody.Part.createFormData("inspectiontime[]", path.getTime());
//            partHashMap.add(Inspectionpoint);
//            partHashMap.add(Inspectiontime);
//        }
        return mService.upPointNum(mUserStorage.getToken(), JSON.toJSONString(req));
    }

    public Observable<CheckNumLineEntity> getCheckNumList(String id) {
        return mService.getCheckNumList(mUserStorage.getToken(), id);
    }


    public Observable<CheckNumEntity> getCheckNumLineList(String id) {
        return mService.getCheckNumLineList(mUserStorage.getToken(), id);
    }


    public Observable<ApplyLaborEntity> getApplyLborList(String id, String date) {
        return mService.getApplyLborList(mUserStorage.getToken(), id, date);
    }


    public Observable<BaseRequestResult> addLabor(String projectId, LaborEntity laborEntities) {
        List<MultipartBody.Part> partHashMap = new ArrayList<>();
        partHashMap.add(MultipartBody.Part.createFormData("projectid", projectId));
        partHashMap.add(MultipartBody.Part.createFormData("date", new SimpleDateFormat("yyyy-MM-dd").format(laborEntities.getDate())));
        for (RankTime.ResultMessageBean entity : laborEntities.getRanks()) {
            MultipartBody.Part Inspectionpoint = MultipartBody.Part.createFormData("serviceid[]", String.valueOf(entity.getId()));
            MultipartBody.Part Inspectiontime = MultipartBody.Part.createFormData("servicepeople[]", String.valueOf(entity.getCount()));
            partHashMap.add(Inspectionpoint);
            partHashMap.add(Inspectiontime);
        }
        return mService.addLabor(mUserStorage.getToken(), partHashMap);
    }


    public Observable<BaseRequestResult> delLabor(String id) {
        return mService.delLabor(mUserStorage.getToken(), id);
    }


    public Observable<BaseRequestResult> upLabor(String projectId, String id, LaborEntity laborEntities) {

        List<MultipartBody.Part> partHashMap = new ArrayList<>();
        partHashMap.add(MultipartBody.Part.createFormData("projectid", projectId));
        partHashMap.add(MultipartBody.Part.createFormData("id", id));
        partHashMap.add(MultipartBody.Part.createFormData("date", new SimpleDateFormat("yyyy-MM-dd").format(laborEntities.getDate())));
        for (RankTime.ResultMessageBean entity : laborEntities.getRanks()) {
            MultipartBody.Part Inspectionpoint = MultipartBody.Part.createFormData("serviceid[]", String.valueOf(entity.getId()));
            MultipartBody.Part Inspectiontime = MultipartBody.Part.createFormData("servicepeople[]", String.valueOf(entity.getCount()));
            partHashMap.add(Inspectionpoint);
            partHashMap.add(Inspectiontime);
        }
        return mService.upLabor(mUserStorage.getToken(), partHashMap);
    }


    public Observable<BaseRequestResult> applyLabor(String projectId,
                                                    int serviced,
                                                    String day,
                                                    int userid,
                                                    String reason,
                                                    String date) {

        Map<String, Object> imgMap = new HashMap<>();
        imgMap.put("projectid", projectId);
        imgMap.put("day", day);
        imgMap.put("serviceid", serviced);
        imgMap.put("userid", userid);
        imgMap.put("reason", reason);
        imgMap.put("date", date);
        return mService.applyLabor(mUserStorage.getToken(), imgMap);
    }


    public Observable<RankTime> gethoursTimeList(String id) {
        return mService.gethoursTimeList(mUserStorage.getToken(), id);
    }

    public Observable<LaborRequestEntity> getLaborList(String id, String date) {
        return mService.getLaborList(mUserStorage.getToken(), id, date);
    }

    public Observable<AllotPersonEntity> getLaborUserList(String projectid, String date) {
        return mService.getLaborUserList(mUserStorage.getToken(), projectid, date);
    }


    public Observable<BaseRequestResult> addPersonalHours(String projectId, List<MultiItemEntity> personEntities, String date) {
        List<MultipartBody.Part> partHashMap = new ArrayList<>();
        partHashMap.add(MultipartBody.Part.createFormData("projectid", projectId));
        partHashMap.add(MultipartBody.Part.createFormData("date", date));
        for (MultiItemEntity entity : personEntities) {
            if (entity instanceof PersonEntity) {
                PersonEntity personEntity = (PersonEntity) entity;
                MultipartBody.Part user = MultipartBody.Part.createFormData("userid[]", String.valueOf(personEntity.getId()));
                List<Integer> serviceList = new ArrayList<>();
                List<Integer> dayList = new ArrayList<>();
                for (PersonLaborEntity laborEntity : personEntity.getSubItems()) {
                    serviceList.add(laborEntity.getId());
                    dayList.add(laborEntity.getCount());
                }
                MultipartBody.Part service = MultipartBody.Part.createFormData("serviceid[]", JSON.toJSONString(serviceList));
                MultipartBody.Part day = MultipartBody.Part.createFormData("day[]", JSON.toJSONString(dayList));
                partHashMap.add(service);
                partHashMap.add(day);
                partHashMap.add(user);
            }
        }
        return mService.addPersonalHours(mUserStorage.getToken(), partHashMap);
    }


    public Observable<PersonRank> getPersonalHourslist(String projectid, String date) {


        return mService.getPersonalHourslist(mUserStorage.getToken(), projectid, date);
    }

    public Observable<BaseRequestResult> schedulingAdd(String projectId, VirtualData data) {
        return mService.schedulingAdd(mUserStorage.getToken(), projectId, JSON.toJSONString(data));
    }

    public Observable<RemoteArrangeEntity> getschedulingList(String projectId, String date) {
        return mService.getschedulingList(mUserStorage.getToken(), projectId, date);
    }

}
